<?php
/**
 * 供应商（基础）
 * (model)
 */

namespace App\Models\Basics;

use App\Models\Product\ProductSku;
use App\Models\Product\ProductSupplier;
use Illuminate\Database\Eloquent\Model;
class Supplier extends Model
{
    protected $table = 'b_supplier';

    public $timestamps = false;

    protected $guarded = [];

    public static $_shipFeeType = [
        1 => '含运费',
        2 => '不含运费',
    ];

    //审核状态
    const AUDIT_NO = 0; //未审核
    const AUDIT_SUCCESS = 1; //已审核
    public static $_auditStatus = [
        self::AUDIT_NO => '未审核',
        self::AUDIT_SUCCESS => '已审核'
    ];

    //供应商状态
    const STATUS_YES = 1; //启用
    const STATUS_NO  = 2; //停用
    public static $_status = [
        self::STATUS_YES => '启用',
        self::STATUS_NO => '停用'
    ];

    //供应商类型
    const TYPE_SKU = 1; //成品
    const TYPE_MATERIAL = 2; //材料
    public static $_type = [
        self::TYPE_SKU => '成品',
        self::TYPE_MATERIAL => '材料'
    ];

    /**
     * 供应商合同
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function supplier_contacts(){
        return $this->hasMany(SupplierContact::class);
    }

    /**
     * 供应商账号
     * @return \Illuminate\Database\Eloquent\Relations\HasOne
     */
    public function supplier_account(){
        return $this->hasOne(SupplierAccount::class);
    }

    /**
     * 供应商文件
     * @return \Illuminate\Database\Eloquent\Relations\HasOne
     */
    public function supplier_file(){
        return $this->hasOne(SupplierFile::class);
    }

    /**
     * 结算方式
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function settlement_method(){
        return $this->belongsTo(SettlementMethod::class,'settlement_method_id');
    }

    //第三个参数是定义此关联的模型(Supplier)在连接表(product_supplier)里的外键名，第四个参数是另一个模型(ProductSku)在连接表里的外键名
    public function sku(){
        return $this->belongsToMany(ProductSku::class,'product_supplier','supplier_id','psku_id')
            ->using(ProductSupplier::class)
            ->withPivot('moq', 'supplier_price', 'unit_price', 'purchase_cycle', 'status');
    }

    public static function options($flag = true){
        $query = self::query()->where('type', self::TYPE_SKU);
        if(!$flag){
            $query = $query->where('audit_status', self::AUDIT_SUCCESS)->where('status', self::STATUS_YES);
        }
        return $query->pluck('name','id');
    }

    public static function getNameById($id,$flag = true)
    {
        $query = self::query()->where('type', self::TYPE_SKU);
        if(!$flag){
            $query = $query->where('audit_status', self::AUDIT_SUCCESS)->where('status', self::STATUS_YES);
        }
        return $query->where('id',$id)->value('name');
    }

    public static function getIdByCode($code,$flag = true)
    {
        $query = self::query()->where('type', self::TYPE_SKU);
        if(!$flag){
            $query = $query->where('audit_status', self::AUDIT_SUCCESS)->where('status', self::STATUS_YES);
        }
        return $query->where('code',$code)->value('id');
    }

}
